

;___ Methods __________________________________________________________________________________




WM_MOUSEMOVE()
{
	global
	BBU := A_BatchLines
	SetBatchLines, -1
	ErrorLevel := 0
	MouseGetPos,,,, thisHwnd, 2
	MouseGetPos,,,, controlname
	ControlGetText, controltext, %controlname% 

	Array := Object()
	Parent := trim(USER_INI.Parents[controltext])
	PrevParent := trim(USER_INI.Parents[PrevText])

	; Still In same control --> Do Nothing 
	if (PREV_HWND = thisHwnd)
		return

	; Just entered a non-button control --> switch previous control to 'non-hover' colors
	if (thisHwnd = backsearch || thisHwnd = "" || controlname = "" || thisHwnd = FOOTER || !InStr(controlName, "static"))
	{
		Array := GET_COLOR(USER_INI[PrevParent][PrevText], 1)
		CTLCOLORS.Change(PREV_HWND, Array[2], Array[1])
		PREV_HWND := ""
		return
	}
	
	; New button --> change the colors..
	Array := GET_COLOR(USER_INI[PrevParent][PrevText], 1)
	CTLCOLORS.Change(PREV_HWND, Array[2], Array[1])
	
	Array := GET_COLOR(USER_INI[Parent][controltext], 0)
	CTLCOLORS.Change(thisHwnd, Array[2], Array[1])
	
	PREV_HWND := thisHwnd
	PrevText := controltext
}



VARSET()
{
	global
	
	USER_INI := new ini(USER_SETTINGS) ; initialize user_settings ini w/ cini.ahk

	PREV_HWND := "" ; when hovering, previous control's hwnd that was hovered over
	
	GUI_SPACING := USER_INI.GuiInfo.Spacing ; gui spacing
	
	BUTTON_WIDTH := USER_INI.GuiInfo.Width ; gui button width
	BUTTON_HEIGHT := USER_INI.GuiInfo.Height ; gui button height
	
	GO_WIDTH := USER_INI.GuiInfo.GoWidth ; go button width
	GO_X := (BUTTON_WIDTH *2) + (GUI_SPACING * 2) - GO_WIDTH ; go button x position
	
	SEARCH := USER_INI.GuiInfo.Search ; search enabled or disabled
	SEARCH_TEXT_SIZE := USER_INI.GuiInfo.SearchText ; search text size
	SEARCH_BACK_TEXT := USER_INI.GuiInfo.BackText ; backtrans search text
	SEARCH_HEIGHT	:= USER_INI.GuiInfo.SearchHeight
	SEARCH_WIDTH := (BUTTON_WIDTH * 2) - GO_WIDTH ; width of search box
	SEARCH_TEXT_WIDTH := SEARCH_WIDTH - 8
	
	FOOTER_WIDTH := (BUTTON_WIDTH * 2) + GUI_SPACING
	FOOTER_SHOW := USER_INI.GuiInfo.Footer
	
	MAIN_BUTTONS := Trim(USER_INI.Keys("MainButtons")) ; gui button labels
	
	TEXT_SIZE := USER_INI.GuiInfo.Text ; gui text size
	TEXT_BOLD := USER_INI.GuiInfo.TextW ; gui text boldness
}


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;GET NUMBER OF KEYS IN INI SECTION

KeyCount()
{
}


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;GET PARENT FROM INI FILE

GetParent(ButtonKey, USER_INI)
{
	return % trim(USER_INI.Parents[ButtonKey])
}


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;DETERMINE IF MOUSE POSITION IS ON LEFT OR RIGHT OF GUI MIDPOINT

ON_LEFT(CONTROL_POSX)
{
	global
	
	MIDPOINT := ((2 * BUTTON_WIDTH) + (3 * GUI_SPACING)) / 2
	
	if (CONTROL_POSX < MIDPOINT)
		return 1
	else
		return 0
}


;~~~~~~~~~~~~~~~~~~~~~~
;GOOGLE SEARCH FUNCTION

Google(GSEARCH)
{
	GSEARCH := RegExReplace(GSEARCH, "i)\s", "+")
	GSEARCH := "http://www.google.com/search?hl=en&source=hp&q=" GSEARCH "&aq=f&aqi=&aql=&oq="
	Run % GSEARCH
}



;
; Function: GetModifiers
; Description:
;		Returns a string of symbols representing which modifier keys are currently being held down.
;			^ = Ctrl
;			! = ALt
;			+ = Shift
;			# = Win
; Syntax: GetModifiers()
; Parameters:
;		None
; Return Value:
;		Returns a string containing the character representation of the modifier keys
; Example:
;		; Assuming Ctrl and Alt are currently held down
;		mods := GetModifiers()
;		MsgBox Current modifiers are %mods%.	; MsgBox Output: Current modifiers are ^!.
;
GetModifiers()
{
Modifiers := GetKeyState("Ctrl", "P") ? "^" : ""
Modifiers .= GetKeyState("Alt", "P") ? "!" : ""
Modifiers .= GetKeyState("Shift", "P") ? "+" : ""
Modifiers .= GetKeyState("LWin", "P") ? "#" : ""

Return, Modifiers
}


MenuIniWrite(me, buttonName, buttonPath, commandLetter, colorSet, NoChild, NoDel)
{
	global
	
	USER_INI[me][buttonName] := "[" commandLetter "] " colorSet " " buttonPath
	USER_INI.Parents[buttonName] := me
	if (NoChild)
		USER_INI.NoChildren[buttonName] := buttonName
	if (NoDel)
		USER_INI.NoDelete[buttonName] := buttonName
	USER_INI.Save(USER_SETTINGS)
}


MenuIniDelete()
{
}


ColorPicker()
{

	MsgBox, 262144, c0bra Colors, Choose the text color for the button
	TextColor := RegExReplace(ColorChooser(), "i)0x")
	if !(TextColor)
		return
	
	MsgBox, 262144, c0bra Colors, Choose the highlight text color for the button
	HLTextColor := RegExReplace(ColorChooser(), "i)0x")
	if !(HLTextColor)
		return
	
	MsgBox, 262144, c0bra Colors, Choose the button color for the button
	ButtonColor := RegExReplace(ColorChooser(), "i)0x")
	if !(ButtonColor)
		return
	
	MsgBox, 262144, c0bra Colors, Choose the highlight button color for the button
	HLButtonColor := RegExReplace(ColorChooser(), "i)0x")
	if !(HLButtonColor)
		return
	
	;~ MsgBox % TextColor ", " HLTextColor ", " ButtonColor ", " HLButtonColor
	SetFormat, integer, d
	return % "{" TextColor ", " HLTextColor ", " ButtonColor ", " HLButtonColor "}"
}


GET_COLOR(COMMAND, REG_COLOR)
{
	Array := Object()

	COMMAND := RegExReplace(COMMAND, "\s+", " ")
	RegExMatch(COMMAND, ";\K.+?$", Description)
	COMMAND := RegExReplace(COMMAND, ";" Description "$")
	COMMAND := Trimmer((Description && ReturnDescription) ? Description : COMMAND)
	
	RegExMatch(COMMAND, "^\[.+\]\s*\{\K(.+),(.+),(.+),(.+)\}", THE_COLOR)
	
	If (REG_COLOR)
	{
		Array.Insert(THE_COLOR1)
		Array.Insert(THE_COLOR3)
	} 
	else 
	{
		Array.Insert(THE_COLOR2)
		Array.Insert(THE_COLOR4)
	}
	
	return Array
}




;~~~~~~~~~~~~~~~~~~~
;EXECUTE THE COMMAND

Execute(Command)
{
	If !Command ; if command is blank
		Return
	
	COMMAND := RegExReplace(COMMAND, "\s+", " ") ; Deletes duplicated spaces and tabs
	RegExMatch(COMMAND, ";\K.+?$", Description) ; Extracts the description if there is one
	COMMAND := RegExReplace(COMMAND, ";" Description "$") ; Removes the description from the command
	COMMAND := Trimmer((Description && ReturnDescription) ? Description : COMMAND) ; Trims beginning/ending spaces and tabs ( = Trim() )
	
	Transform, Command, Deref, % Command
	
	;~~~~~~~~~~~~~~~~~~~~~~
	;[F] FUNCTION AS ACTION
	
		If (RegExMatch(Command, "i)^\[F\]\s*\{.*\}\s(?P<Func>\w+)(\(\s*(?P<Params>.+?)\s*\))?", m) && IsFunc(mFunc))
		{
			If mParams
			{
				Loop, Parse, mParams, CSV
					param%A_Index% := Trimmer(A_LoopField)
			}
			
			%mFunc%(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)
			Gui, 1:Destroy
		}
	
	;~~~~~~~~~~~~~~~~~~~
	;[L] LABEL AS ACTION
	
		Else If (RegExMatch(Command, "i)^\[L\]\s*\{.*\}\s\K.+", Label) && IsLabel(Label))
		{
			Gui, 1:Destroy
			Gosub, %LABEL%
		}
		
	;~~~~~~~~~~~~~~~~~~~~~~
	;[S] SHORTCUT AS ACTION
	
		Else If RegExMatch(COMMAND, "^\[M\]\s*\{.*\}\s\K.+", SHORTCUT)
		{
			Gui, 1:Destroy
			SendInput, {Blind}%SHORTCUT%
		}
		
	;~~~~~~~~~~~~~~~~~~~
	;[M] MACRO AS ACTION
	
		Else If RegExMatch(Command, "i)^\[M\]\s*\{.*\}\s\K.+", MACRO)
		{
			Gui, 1:Destroy
			MACRO .= "`n`nsleep, 50`n`nExit App"
			FileDelete, tmpMacroFile.ahk
			FileAppend, %Macro%, tmpMacroFile.ahk
			RunWait, tmpMacroFile.ahk`
		}
	
	;~~~~~~~~~~~~~~~~~~
	;[T] TEXT AS ACTION
		
		Else If RegExMatch(Command, "i)^\[T\]\s*\{.*\}\s\K.+", TEXT)
		{
			Gui, 1:Destroy
			StringReplace, TEXT, TEXT, ``n, `n, All
			StringReplace, TEXT, TEXT, ``r, `r, All
			StringReplace, TEXT, TEXT, ``t, %A_Tab%, All
			SendInput, {Raw}%TEXT%
		}
		
	;~~~~~~~~~~~~~~~~~~~~~~
	;[G] SIDE GUI AS ACTION
		
		else if RegExMatch(Command, "i)^\[G\]\s*\{.*\}\s\K.+", NEWGUI)
			SIDE_GUI(NEWGUI)
			
	;~~~~~~~~~~~~~~~~~~~~~
	;[P] PROGRAM AS ACTION
	
		else if RegExMatch(Command, "i)^\[P\]\s*\{.*\}\s\K.+", THE_PROGRAM)
		{
			Gui, 1:Destroy
			SetTitleMatchMode, 2
			
			THE_PROGRAM_NOEXT := RegExReplace(THE_PROGRAM, "i).exe")

			IfWinExist % THE_PROGRAM_NOEXT ; check if it exists already
				WinActivate % THE_PROGRAM_NOEXT
			else
				Run % THE_PROGRAM
			
			SetTitleMatchMode, 1
		}
		
	;~~~~~~~~~~~~~~~~~~~~~
	;[E] EXPLORE AS ACTION
	
		else if RegExMatch(Command, "i)^\[E\]\s*\K.+", EXPLORE_PATH)
		{
			Gui, 1:Destroy
			run explorer.exe %EXPLORE_PATH%
		}
		
	;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	;[AHK] EDIT AHK WITH AHK EDITOR
	
	
		else if RegExMatch(Command, "i)^\[AHK\]\s*K.+", AHK_PATH)
		{
			Gui, 1:Destroy
			AHK(AHK_PATH)
		}
			
	;~~~~~~~~~~~
	;RUN ACTION
	
		Else
		{
			Gui, 1:Destroy
			Run(Command)
		}
}


;~~~~~~~~~~~~~~~~~
;AHK EDIT FUNCTION

AHK(AHK_PATH)
{
	global
	
	Run % AHKEDITOR " " AHK_PATH
}


;~~~~~~~~~~~~~~~~
;TRIMMER FUNCTION

Trimmer(str, omitchars=" `t")
{ ; Allow to use Trim() with AutoHotkey basic
	If !StrLen(omitchars)
		Return, str
	str := RegExReplace(str, "^[" omitchars "]+") ; Trims from the beginning
	str := RegExReplace(str, "[" omitchars "]+$")	; Trims from the end
	Return, str
}


;~~~~~~~~~~~~
;RUN FUNCTION

Run(Path)
{
	Run, % Path,, UseErrorLevel
	If ErrorLevel
		MsgBox, 262160, Run :, Error with this command!`n%Path%, 1
}

